package com.chery.media.ui.setting.gps

import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.chery.media.R
import com.chery.media.databinding.FragmentGpsBinding
import com.chery.media.library.base.BaseVMFragment

class GpsFragment: BaseVMFragment<GpsViewModel, FragmentGpsBinding>() {

    private var register: ActivityResultLauncher<Intent>? = null

    override val layoutId: Int
        get() = R.layout.fragment_gps


    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        clickListener()
//        val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS, Uri.parse("package:com.chery.media"))
//            register?.launch(intent)
//        startActivityForResult(intent, 1000)
//        viewModel.getGpsStatus(requireActivity())
    }

    override fun onResume() {
        super.onResume()
        viewModel.getGpsStatus(requireActivity())
    }

    override fun initObserve() {
        viewModel.gpsStatus.observe(this) {
            mBinding.gpsSwitch.toggleSwitch(it)
        }
        register = registerForActivityResult(
            ActivityResultContracts.StartActivityForResult()
        ) {

        }
    }

    private fun clickListener(){
        mBinding.gpsSwitch.setOnClickListener {
            val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
            startActivity(intent)
//            viewModel.setGpsStatus(requireActivity(), mBinding.gpsSwitch.isOpened)
        }
    }

    companion object {
        fun newInstance() = GpsFragment()
    }

}